package com.lagou.zuoyes1m3;

import java.util.*;

public class Dou {

    //打印牌
    public static void find(HashMap<Integer, String> hashMap,TreeSet<Integer> ts,String name) {
        System.out.print(name+"的牌是：");

          //方法一：将集合转换成数组，然后从后面遍历，从hashmap查找
//        Object[] objects = ts.toArray();
//        for (int i = objects.length - 1; i >= 0; i--) {
//            System.out.print(hm.get(objects[i])+"  ");
//        }

        //方法二：从集合后面开始打印，打印一个删一个，这样集合长度就一直在减小
        for (int i = ts.size() - 1; i >= 0; i--) {
            //从最后一个开始将查到的元素，添加到HashMap集合并打印
            System.out.print(hashMap.get(ts.last())+"  ");
            //删除元素
            ts.remove(ts.last());
        }

        //从小到大打印
//        for(Integer i : ts) {
//            System.out.print(hm.get(i)+"  ");
//        }
        System.out.println();
    }

    public static void main(String[] args) {

        //两个集合，一个代表扑克的数字，另一个代表花色
        String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] color = {"♠","♣","♦","♥"};

        //创建HashMap集合用来存储花色数字的组合，和在哪个位置
        HashMap<Integer, String> hashMap = new HashMap<>();

        //创建ArrayList集合来定位HashMap中的牌
        List<Integer> list = new ArrayList<>();

        //遍历两个数组，将牌存到HashMap集合
        int index = 0;
        for (String s1 : num) {
            for (String s2 : color) {
                hashMap.put(index, s2.concat(s1));
                list.add(index);
                index++;
            }
        }
        //大小王放到最后
        hashMap.put(index, "小王");
        list.add(index);
        index++;
        hashMap.put(index, "大王");
        list.add(index);

        //洗牌
        Collections.shuffle(list);
        //System.out.println(list);

        //用reeSet能够保证集合从小到大排列
        TreeSet<Integer> p1 = new TreeSet<>();
        TreeSet<Integer> p2 = new TreeSet<>();
        TreeSet<Integer> p3 = new TreeSet<>();
        TreeSet<Integer> dp = new TreeSet<>();

        for (int i = 0; i < list.size(); i++) {
            //一共54长牌，从0开始，如果≥51，说明就剩底牌了
            if(i >= 51){
                dp.add(list.get(i));
                //剩余的分给玩家几号都可以
            }else if(i % 3 == 0){
                p1.add(list.get(i));
            }else if(i % 3 == 2){
                p2.add(list.get(i));
            }else {
                p3.add(list.get(i));
            }
        }

        //调用查看方法，打印牌
        find(hashMap, p1, "玩家一号");
        find(hashMap, p2, "玩家二号");
        find(hashMap, p3, "玩家三号");
        find(hashMap, dp, "底    牌");
    }
}
